package ru.hh.school.commands;

import ru.hh.school.User;
import ru.hh.school.exceptions.InnerChatException;
import ru.hh.school.exceptions.MessageNotFoundException;
import ru.hh.school.logging.Logger;
import ru.hh.school.logging.StaticLogger;
import ru.hh.school.persistence.DataRepository;

import static ru.hh.school.util.ResourceManager.message;

public class ConfirmCommand extends Command {
    private final DataRepository dataRepository;

    public ConfirmCommand(Logger logger, DataRepository dataRepository) {
        super(logger);
        this.dataRepository = dataRepository;
    }

    public String getCommand() {
        return "/confirm";
    }

    public String getDescription() {
        return message("command.confirm.description");
    }

    public boolean isPublicCommand() {
        return false;
    }

    public void process(User user, String unparsedMessageId) throws InnerChatException, InterruptedException {
        long messageId;
        int userId;
        messageId = Long.parseLong(unparsedMessageId);
        try {
            userId = dataRepository.getRecipientIdByPrivateMessageId(messageId);
        } catch (MessageNotFoundException e) {
            StaticLogger.getLogger().logError(e);
            user.sendMessage(message("command.confirm.wrong.protocol"));
            return;
        }
        if (userId != user.getId()) {
            user.sendMessage(message("command.confirm.wrong.protocol"));
        } else {
            dataRepository.deletePrivateMessage(messageId);
        }
    }
}